Skip to main content

Q-2 Alternative: Views vs ViewGroups OR Checkboxes and Radio Groups (5 Marks)

Questions​

a) Differentiate between Views and ViewGroups in the context of layouts (5 marks)

OR

b) Explain the use of Checkboxes and Radio Groups in user interfaces with examples (5 marks)


Answers​

a) Differentiate between Views and ViewGroups​

AspectViewsViewGroups
DefinitionBasic building blocks of UI that occupy rectangular space on screenInvisible containers that hold and arrange other Views/ViewGroups
PurposeDisplay content and handle user interactionsOrganize and position child elements
InheritanceInherit directly from View classInherit from ViewGroup class (which extends View)
ContentCannot contain other viewsCan contain multiple child Views/ViewGroups
ExamplesTextView, Button, ImageView, EditTextLinearLayout, RelativeLayout, ConstraintLayout, FrameLayout
FunctionalityPerform specific tasks (display text, handle clicks)Define layout rules and positioning
VisibilityVisible to usersUsually invisible (background containers)
InteractionDirect user interactionIndirect interaction through child views

Key Characteristics:​

Views:

  • Atomic Elements: Cannot be broken down further
  • User Interaction: Handle touch events, clicks, focus
  • Rendering: Draw specific content (text, images, buttons)
  • Properties: Width, height, padding, margin, background
  • Examples in Code:
<TextView android:text="Hello World" />
<Button android:text="Click Me" />
<ImageView android:src="@drawable/image" />

ViewGroups:

  • Container Elements: Hold and organize other elements
  • Layout Management: Control positioning and sizing of children
  • Nesting: Can contain other ViewGroups (nested layouts)
  • Layout Parameters: Define how children are arranged
  • Examples in Code:
<LinearLayout android:orientation="vertical">
<TextView android:text="Title" />
<Button android:text="Action" />
</LinearLayout>

b) Use of Checkboxes and Radio Groups with Examples​

Checkboxes​

Purpose: Allow users to select multiple options from a set of choices.

Key Features:

  • Multiple Selection: Users can check/uncheck multiple items
  • Independent: Each checkbox operates independently
  • Binary State: Either checked or unchecked
  • Persistence: Maintains state until changed by user

Use Cases:

  • Settings/Preferences: Enable/disable features
  • Forms: Accept terms, subscribe to newsletter
  • Filters: Select multiple filter criteria
  • To-do Lists: Mark completed tasks

Example Implementation:

<CheckBox
android:id="@+id/checkboxJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java Programming"
android:checked="false" />

<CheckBox
android:id="@+id/checkboxKotlin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin Programming"
android:checked="true" />

Java Code:

CheckBox javaCheckBox = findViewById(R.id.checkboxJava);
javaCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// Handle checked state
}
}
});

Radio Groups​

Purpose: Allow users to select exactly one option from a set of mutually exclusive choices.

Key Features:

  • Single Selection: Only one radio button can be selected at a time
  • Mutual Exclusion: Selecting one automatically deselects others
  • Grouped: RadioButtons must be inside a RadioGroup
  • Visual Feedback: Clear indication of selected option

Use Cases:

  • Gender Selection: Male/Female/Other
  • Payment Methods: Credit Card/Debit Card/PayPal
  • Priority Levels: High/Medium/Low
  • Survey Questions: Single-choice answers

Example Implementation:

<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />

<RadioButton
android:id="@+id/radioOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other" />
</RadioGroup>

Java Code:

RadioGroup radioGroup = findViewById(R.id.radioGroupGender);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioMale:
// Handle male selection
break;
case R.id.radioFemale:
// Handle female selection
break;
}
}
});

Best Practices:​

  • Use Checkboxes when users can select multiple options
  • Use Radio Groups when users must select exactly one option
  • Provide clear labels for all options
  • Group related options logically
  • Consider default selections based on common user preferences